package org.boot.controller;

import jakarta.annotation.Resource;
import org.boot.dao.CityRepository;
import org.boot.dao.MyBean;
import org.boot.dao.UserDao;
import org.boot.entity.City;
import org.boot.entity.User;
import org.boot.event.HelloApplicationEvent;
import org.boot.event.MyEventPublisher;
import org.boot.redis.RedisBean;
import org.boot.service.AccountService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserDao userDao;


    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    // localhost:9090/user/get
    @RequestMapping("/get")
    String get() throws InterruptedException {
        User user = userDao.selectForUpdate(1);
        Thread.sleep(3000);
        if (Objects.nonNull(user)) {
            user.setIdNo("34155874");
            userDao.save(user);
        }
        Thread.sleep(2000);
        return "34155874";
    }

    @RequestMapping("/set")
    String set() {
        Optional<User> optional = userDao.findById(1);
        String idno = UUID.randomUUID().toString();
        if (optional.isPresent()) {
            User u = optional.get();
            u.setIdNo(idno);
            userDao.save(u);
            System.out.println(u.getIdNo());
        }
        return idno;
    }

}
